//存入Token
import {JSEncrypt} from "jsencrypt";

export const setToken = (key:string, token: string) => {
    localStorage.setItem(key, token);
}
//获取token
export const getToken = (key:string) => {
    return localStorage.getItem(key);
}
//清除token
export const removeToken = (key:string) => {
    localStorage.removeItem(key);
}

//防抖函数
export const debounce=(func:Function, delay:number) =>{
    let timer:any = null;
    return function (...args:any[]) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            // @ts-ignore
            func.apply(this, args);
        }, delay);
    };
}

//自动获取位置
export default function loadBMap(ak:String) {
    return new Promise(function(resolve, reject) {
        if (typeof window.BMap !== 'undefined') {
            resolve(window.BMap)
            return true
        }
        window.onBMapCallback = function() {
            resolve(window.BMap)
        }
        const script = document.createElement('script')
        script.type = 'text/javascript'
        script.src =
            'https://api.map.baidu.com/api?v=3.0&ak=' + ak + '&callback=onBMapCallback' //ak表示自己的申请的ak
        script.onerror = reject
        document.head.appendChild(script)
    })
}

//加密密码
export const encryptPassword = (password:string) => {
    const publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuHd4dIO5XWcU6nPdffi0RzPtuAVciQyCuC4AZddT8VfTsYphfXu3aFthF6F70efpuDfH0UZkFgxyiqo7dZsXJsXB+S/IXtrIQnjhSlrqJ66K2WGE3u03NuIYtLCx3UDUEx5q1TDM16dsBb0H2XMnOeq4PDUshZ1KOo76O2dGPDSZaw5eKPqnILNkZYOV7NtRmJ5HcWF5ipD0jpDheoRa2Ppkx49VN88hzNqXH/B/HRe++7BYqD2xWbfM3T2VxkZlRpRbgXizgWsdq366dTJYcP96qG4wIRTydikqDzQ2EOt+/f2FU03qgiVykD5+s7olVHLb1S7EmF+Yexe1yJthBQIDAQAB";

    const encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    return encrypt.encrypt(password);
};


